<?php
/**
 * xml简单操作类
 * @author Administrator
 *
 * $xml = new CI_Xml($file);
 * 添加节点=》 根节点的子节点
 * $xml->addNode('dict',array('AppName'=>$appName,'ShopID'=>$_SESSION['shop_id'],'text'=>array('AppName'=>$appName,'ShopID'=>$_SESSION['shop_id'])));
 * $xml->saveFile();
 *查找节点
 *     $select = $this->xml->searchByItem('ShopID','100033');
 * 创建节点
		$newNode = $this->xml->createNode('ShopIDD',$_SESSION['shop_id']);
	插入节点=>新节点插入查找节点的前面
		$>xml->insertNode($newNode,$select[0],'before');
 * 修改节点的值
 * $xml->updataNode($select,'asdasd');
 * $xml->saveFile(); //修改插入都要保存
 */
class CI_Xml  extends DOMDocument {
	private static $version = '1.0'; //xml版本
	private static $encoding = 'UTF-8';//编码
	private $filename = null;
	private $rootNode = null;
	/***
	 *构造函数
	 **/
	public function __construct($option) {
		parent::__construct(self::$version,self::$encoding);
		$this->formatOutput = true;
		if(!is_array($option)){
			die("文件路径与根节点名称");
		}
		$this->filename = $option['file'];
		if(!file_exists($this->filename)){
			die("该配置文件不存在");
		}
		$this->load($this->filename);
		$this->__checkRootNode($option['rootNode']);
	}
	/**
	 * 获取根节点
	 * @param unknown $rootNode
	 */
	private function __checkRootNode($_rootNode){
		if(is_null($_rootNode)){
			die("设置根节点");
		}else{
			$this->rootNode =$this->getElementsByTagName($_rootNode)->item(0);
			if(is_null($this->rootNode)){
				$this->rootNode = $this->createElement($_rootNode);
				$this->rootNode->setAttribute('version',"1.0");
				$this->appendChild($this->rootNode);
			}
		}
		$this->rootNode->formatOutput = true;
	}
	/**
	 * 创建节点
	 * @param unknown $data
	 * @return boolean|unknown
	 */
	public function createNode($key ,$val){
		return	$this->createElement($key ,$val);
	}
	/**
	 * 增加节点
	 * @param unknown $array 节点数据数组
 	 * @param string $rootnode 根节点
	 */
	public function addNode($nodeName ,$value){
		$elem = $this->_addNode($nodeName ,$value);
		$this->rootNode->appendChild($elem);
		return $this->rootNode ;
	}
	/**
	 * 增加节点
	 * @param unknown $key
	 * @param unknown $val
	 * @return unknown
	 */
	public function _addNode($key,$val){
		if(!is_array($val)){
			$elem = $this->createElement($key,$val);
		}else{
			$elem = $this->createElement($key);
			foreach ( $val as $k =>$v ){
				if(is_array($v)){
					$el = $this->_addNode($k ,$v);
				}else{
					$el = $this->createElement($k,$v);
				}
				$elem->appendChild($el);
			}
		}
		return $elem;
	}
	/**
	 * 插入节点
	 * @param unknown $newNode
	 * @param unknown $refNode
	 * @param string $insertMode
	 */
	public function insertNode( $newNode, $refNode, $insertMode=null){
		if(!$insertMode || $insertMode == "inside") {

			$refNode->appendChild($newNode);

		} else if($insertMode == "before") {
			$refNode->parentNode->insertBefore($newNode, $refNode);

		} else if($insertMode == "after") {

			if($refNode->nextSibling) {
				$refNode->parentNode->insertBefore($newNode, $refNode->nextSibling);
			} else {
				$refNode->parentNode->appendChild($newNode);
			}
		}
	}
	/**
	 * 根据元素查找获取数据
	 * @param unknown $elem
	 * @param string $rootnode
	 * @return array
	 */
	public function searchByItem($key,$val){
		$result =  array();
		$nodes= $this->getElementsByTagName($key);
		foreach ($nodes  as $item) {
			if($item->nodeValue == $val ){
				$result[] = $item;
			}
		}
		return $result;
	}
	/**
	 * 更新节点
	 * @param DOMElement $node
	 * @param unknown $data
	 */
	public function updataNode(DOMElement $node,$val){
		if(is_null($node)){
			return false;
		}
		$this->_updata($node,$val);
		return true;
	}
	/**
	 * 更新数据
	 * @param DOMElement $node
	 * @param unknown $val
	 */
	private function _updata(DOMElement $node,$val){
		$hasChild = $node->hasChildNodes();
		if($hasChild){
			if(is_array($val)){
				foreach ($val as $k=>$v ){
					$items = $node->getElementsByTagName($k);
					foreach ($items as $item) {
						$this->_updata($item,$v);
					}
				}
			}else{
				$node->nodeValue= $val;
			}
		}else{
			$node->nodeValue= $val;
		}
	}
	/**
	 * 删除节点
	 * @param DOMElement $node
	 * @return boolean
	 */
	public function delNode(DOMElement $node){
		if(is_null($node)){
			return false;
		}
		$parentNode = $node->parentNode;
		$result = $parentNode->removeChild ($node);
		return true;
	}
	/**
	**保存文件
	**/
	public function saveFile(){
		return   $this->save($this->filename);
	}
}
?>
